/**
 * ...
 * >author		João Vítor de Souza
 */

package com.cupcakeengine.data 
{
	import flash.net.SharedObject;
	import flash.utils.Dictionary;

	public class SharedObjectPersist 
	{
		// ___________________________________________________________________ CONSTANTS	
		
		// ___________________________________________________________________ CLASS PROPERTIES
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		
		private static var _instance		: SharedObjectPersist;
		private var _sharedObject			: SharedObject;
		private var _data					: Dictionary;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		
		// ___________________________________________________________________ CONSTRUCTOR
		
		public function SharedObjectPersist (singleton:SingletonObligate) {}
		
		// ___________________________________________________________________ PUBLIC METHODS
		
		public static function getInstance () : SharedObjectPersist
		{
			if (!SharedObjectPersist._instance) SharedObjectPersist._instance = new SharedObjectPersist(new SingletonObligate());
			return SharedObjectPersist(SharedObjectPersist._instance);
		}
		
		public function init(name:String, localPath:String = null, secure:Boolean = false):void
		{
			if (_sharedObject == null) _sharedObject = SharedObject.getLocal(name, localPath, secure)
			if (_data == null) _data = new Dictionary();
		}
		
		public function getData(id:String):*
		{
			if (_sharedObject.data[id] != undefined) _data[id] = _sharedObject.data[id];
			
			return _data[id];
		}
		
		public function setData(id:String, data:*):void
		{
			_sharedObject.data[id] = data;
			_data[id] = data;
		}
		
		
		// ___________________________________________________________________ PRIVATE METHODS
		
		// ___________________________________________________________________ EVENTS
	}
}

class SingletonObligate {}